package chapter05

object Test10_Recursion {
  def main(args: Array[String]): Unit = {
    println(fact(-1))
  }

  def fact(n: Int): Int = {
    if (n < 0) throw new RuntimeException("Insert error")
    if (n == 0) return 1
    fact(n - 1) * n
  }

  //尾递归调用
  def tailFact(n: Int): Int = {
    def loop(n: Int, currRes: Int): Int = {
      if (n == 0) return currRes
      loop(n - 1, currRes * n)
    }

    loop(n, 1)
  }

}
